var app = angular.module('myApp',[]);
//获取验证码
var chars=['A','B','C','D','E','F','G','H','J','K','L','M','N','P'
    ,'Q','R','S','T','U','V','W','X','Y','3','4','5','6','7','8','9'];
var getAuthCode = function () {
    var str = '';
    for(var i=0 ;i < 4 ; i++){
        str += chars[parseInt(Math.random() * 30)];
    }
    return str;
}


app.controller('loginController',['$scope','$http',function($scope,$http){
    //$scope.username = 'admin';
    //$scope.password = '123456';
    $scope.authcode = getAuthCode();
    var data = {
        username:$scope.username,
        password:$scope.password
    }
    $scope.login = function () {
        if( $scope.username == null || $scope.username == ''){
            alert("用户名不允许为空");
            return;
        }

        if($scope.password == null || $scope.password == ''){
            $("#passwordMsg").show();
            return;
        }else {
            $("#passwordMsg").hide();
        }

        if($scope.testauthcode == null
            || $scope.testauthcode == ''
            ||  $scope.testauthcode.toString().toLowerCase() != $scope.authcode.toString().toLowerCase()){
            $scope.testauthcode = '';
            $('#testauthcode').popover('show');
            return;
        }else {
            $('#testauthcode').popover('hide');
        }

        $http.post('/login',data).then(function (value) {
            alert(value);
        })
    }

    $scope.changeAuthCode = function(){
        $scope.authcode = getAuthCode();
    }

}]);